package cn.linstudy.blog.utils;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;

/**
 * @Author XiaoLin
 * @Date 2021/12/22 16:54
 * @Description
 */
public class BeanConvertUtil<T>{

    /**
     * DO转换为DTO
     * DTO转换为DO
     * VO转换为DTO
     * DTO转换为VO
     *
     * @param startObj
     * @return
     */
    public List<T> convert(List<T> startObj, Class<T> clazz) {
        List<T> endObj = new ArrayList<>();

        for (T obj : startObj) {
            T t = null;
            try {
                t = clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            BeanUtils.copyProperties(obj, t);
            endObj.add(t);
        }
        return endObj;
    }

    @SuppressWarnings("rawtypes")
    public static BeanConvertUtil convertInstance() {
        return new BeanConvertUtil();
    }
}
